import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard


def run_game():
    # py game 初始化
    pygame.init()
    # 读取配置
    ai_settings = Settings()

    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption(ai_settings.game_name)
    # 创建一艘飞船
    ship = Ship(ai_settings, screen)
    # 创建一个用于存储子弹的数组
    bullets = Group()
    # 创建一个外星人组
    aliens = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)
    # 创建一个用于存储游戏统计信息的实例
    stats = GameStats(ai_settings)
    # 创建Play按钮
    play_button = Button(screen, "Play")
    # 创建一个记分牌
    scoreboard = Scoreboard(ai_settings, screen, stats)
    # 开始游戏主循环
    while True:
        # 检查按键
        gf.check_events(ai_settings, scoreboard, screen, ship, stats, aliens, bullets, play_button)
        if stats.game_active:
            # 更新
            ship.update()
            gf.update_bullets(ai_settings, scoreboard, screen, ship, stats, aliens, bullets)
            gf.update_aliens(ai_settings, scoreboard, screen, ship, stats, aliens, bullets)
        # 刷新屏幕
        gf.update_screen(ai_settings, scoreboard, screen, ship, stats, aliens, bullets, play_button)


run_game()
